<?php
/**
 * Created by lzl
 * Date: 2021 2021/9/14
 * Time: 9:41
 */

namespace App\Http\Controllers\Admin;

use App\Exceptions\RequestException;
use App\Forms\ArticleForms;

class ArticleController extends Controller
{

    /**
     * 文章列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws RequestException
     */
    public function articleList()
    {
        if($this->request->ajax()) {
            $rules = [
                'page'       => 'required',
                'limit'      => 'required',
            ];

            $this->validateInput($rules);

            return $this->showJsonLayui((new ArticleForms())->articleList($this->validated));
        }

        return $this->view('article.article_list');
    }


    /**
     * 修改文章
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     * @throws RequestException
     */
    public function editArticle()
    {
        $rules = [
            'id'          => 'nullable',
            'title'       => 'nullable',
            'author'      => 'nullable',
            'description' => 'nullable',
            'content'     => 'nullable',
            'images'      => 'nullable',
        ];

        $this->validateInput($rules);

        $form = new ArticleForms();

        if($this->request->isMethod('post')) {

            return $this->success($form->editArticle($this->validated));
        }

        return $this->view('article.edit_article',  $form->getArticleOrNew($this->validated));
    }


}